<?

/** dzkone lockset functionality.
 *
 *  This collection of functions (libary) is licensed under GPL2.
 *  See LICENSE or www.gnu.org for more details.
 *
 *  @author: Jyry Kuukkanen + Henri Ranki 11.6.2001
 *  $Id: dzkone_lock.php,v 1.4 2005/03/12 17:06:54 jyry Exp $
 */

/* Include files */
include_once("sodebug.php");


/** Locks a set.
 *  @param dzKone $Kone dzKone object to operate with.
 *  @param SOLM_* $Method Locking method, see sodbrowlock for details.
 *  @param string $CacheFilename Optional cache file name to delete when
 *      unlocking
 *  @return Returns true(1) if lock got acquired, otherwise returns false(0).
 */
function __dzkone__lockSet(&$Kone, $Method, $CacheFilename = "") {
    include("sodebug.inc");
    soDebug("__dzkone__lockSet(Kone, ".$Method.", ".
            $CacheFilename."): enter", SOD_DET);

    if (soGetStreamType($CacheFilename) == "file") {
        $CacheFilename = soExtractVal($CacheFilename, "://", 2);
    }; // if

    $Kone->__setLockParams();
    if ($Method == SOLM_UNLOCK) {
        soDebug("__dzkone__lockSet: deleting cache file ".
                "'$CacheFilename'", SOD_LO);
        @unlink($CacheFilename);
    }; // if SOLM_UNLOCK

    /** try lock/unlock and check whether it succeeded or failed */
    $lock_status = $Kone->__Lock->execute($Method);
    soDebug("__dzKone__lockSet: lock status=$lock_status", SOD_DET);

    if ((($Method == SOLM_UNLOCK) && ($lock_status != SOLS_NONE)) ||
        (($Method == SOLM_NORMAL) && ($lock_status != SOLS_OWN))) {
        soDebug("dzKone: Lock release failed", SOD_LO);

        /* failed - inform caller */
        $Kone->__OutItu->setBlockItem(DZI_CONTROL,
                                    DZK_RESULT,
                                    DZR_LOCK_ERROR);
        $Kone->__OutItu->setBlockItem(DZI_CONTROL,
                                    DZK_RESULT,
                                    DZR_LOCK_ERROR);
        $Kone->__OutItu->setBlockItem(DZI_CONTROL,
                                    DZK_STATUS,
                                    $lock_status);
        $Kone->__OutItu->setBlockItem(DZI_CONTROL,
                                    DZK_STATUS_TEXT,
                                    $Kone->__Lock->Owner."/".
                                    $Kone->__Lock->Session);
        $result = 0;
    } else {
        soDebug("__dzkone__lockSet: Un-/locking ok", SOD_HI);
        $Kone->__OutItu->setBlockItem(DZI_CONTROL,
                                    DZK_RESULT,
                                    DZR_OK);
        $Kone->__OutItu->setBlockItem(DZI_CONTROL,
                                    DZK_STATUS,
                                    $Kone->__Lock->readStatus());
        $Kone->__OutItu->setBlockItem(DZI_CONTROL,
                                    DZK_STATUS_TEXT,
                                    $Kone->__Lock->LockInfo->Owner."/".
                                    $Kone->__Lock->LockInfo->Session);
        $result = 1;
    } // if

    return $result;
}; // __dzkone__lockSet


?>
